<?php

/**
 * Esta clase implementa los hooks, las clases que modifican el cart deben hederar de esta.
 */
class CartWS_Model_FormulaEngine_Table extends Ext_Db_Table {


    private function setChanged( $rowSet ) {
        if ( count( $rowSet ) == 0 ) {
            return;
        }
        foreach( $rowSet as $row ) {

            if ( isset( $row->sales_order_id ) ) {
                $orderId = $row->sales_order_id;
            } else { // Sales line property
                if ( isset( $row->sales_line_id ) ) {
                    $db = new CartWS_Model_SalesOrderLine( $this->_db );
                    $salesLines = $db->find( $row->sales_line_id );
                    if ( count( $salesLines ) != 1 ) {
                        throw new Exception( "sales line not found or too many rows found");
                    }
                    $orderId = $salesLines->current()->sales_order_id;
                } else {
                    throw new Exception( "sales order id needed");
                }
            }
            
            CartWS_Model_FormulaEngine_Engine::getInstance()->setChanged( $this->info( Zend_Db_Table::NAME ), $orderId );
        }
    }

    /**
     * Deletes existing rows. HOOK PARA EL ENGINE DE PROMOCIONES/COSTOS DE ENVIO/RECARGOS POR METODO DE PAGO
     *
     * @param  array|string $where SQL WHERE clause(s).
     * @return int          The number of rows deleted.
     */
    public function delete( $where )
    {
        $rowSet = $this->fetchAll( $where );
        $this->setChanged( $rowSet );
        $ret = parent::delete( $where );
        return $ret;
    }

    /**
     * unDeletes existing rows if this table supports end_date. HOOK PARA EL ENGINE DE PROMOCIONES/COSTOS DE ENVIO/RECARGOS POR METODO DE PAGO
     *
     * @param  array|string $where SQL WHERE clause(s).
     * @return int          The number of rows deleted.
     */
    public function unDelete($where)
    {
        $ret = parent::unDelete( $where );
        if ( $ret > 0 ) {
            $rowSet = $this->fetchAll( $where );
            $this->setChanged( $rowSet );
        }
        return $ret;
    }

    /**
     * Inserts a new row. HOOK PARA EL ENGINE DE PROMOCIONES/COSTOS DE ENVIO/RECARGOS POR METODO DE PAGO
     *
     * @param  array  $data  Column-value pairs.
     * @return mixed         The primary key of the row inserted.
     */
    public function insert(array $data)
    {
        $ret = parent::insert( $data );
		$rowSet = call_user_func_array( array( $this, "find" ), $ret );
        if ( count( $rowSet ) > 0 ) {
            $this->setChanged( $rowSet );
        }
        return $ret;
    }

    /**
     * Updates existing rows. HOOK PARA EL ENGINE DE PROMOCIONES/COSTOS DE ENVIO/RECARGOS POR METODO DE PAGO
     *
     * @param  array        $data  Column-value pairs.
     * @param  array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses.
     * @return int          The number of rows updated.
     */
    public function update(array $data, $where)
    {
        $ret = parent::update( $data, $where );
        if ( $ret > 0 ) {
            $rowSet = $this->fetchAll( $where );
            $this->setChanged( $rowSet );
        }
        return $ret;
    }


}
